package com.example.smartcity.Controller;

import com.alibaba.fastjson.JSONObject;
import com.example.smartcity.Entity.Comment;
import com.example.smartcity.Entity.getComment;
import com.example.smartcity.Entity.getVideoComment;
import com.example.smartcity.Service.NewsCommentService;
import com.example.smartcity.Service.NewsService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.awt.*;

@RestController
public class CommentController {

    @Autowired
    @Qualifier(value = "NewsCommentService")
    private NewsCommentService newsCommentService;

    @RequestMapping(value = "/press/presscomment",method = RequestMethod.POST)
    public String presscomment(@RequestBody String json) {
        // 在这里处理传递过来的字符串
        Gson gson = new Gson();
        getComment gc =  gson.fromJson(json, getComment.class);
        int textid = newsCommentService.findNewsIdByTitle(gc.getTextname());
        System.out.println("newsid: "+textid +"   userid: "+ gc.getUserId() + "   content: "+gc.getContent());
        int userid = gc.getUserId();
        String content = gc.getContent();
        newsCommentService.putCommentToDB(textid,userid,content);
        JSONObject map = new JSONObject();
        map.put("code",200);
        map.put("msg","查询成功");
        return JSONObject.toJSONString(map);
    }

    @RequestMapping(value = "/press/getcomment",method = RequestMethod.GET)
    public String getComment(@RequestParam("textname")String textname){
        int newsid = newsCommentService.findNewsIdByTitle(textname);
        return newsCommentService.findCommentByNewsId(newsid);
    }
    @RequestMapping(value = "/press/pressvideocomment",method = RequestMethod.POST)
    public String pressvideocomment(@RequestBody String json) {
        Gson gson = new Gson();
        getVideoComment gc =  gson.fromJson(json, getVideoComment.class);
        System.out.println("videoid: "+gc.getVideoid() +"   userid: "+ gc.getUserId() + "   content: "+gc.getContent());
        int userid = gc.getUserId();
        int videoid = gc.getVideoid();
        String content = gc.getContent();
        newsCommentService.putVideoCommentToDB(videoid,userid,content);
        JSONObject map = new JSONObject();
        map.put("code",200);
        map.put("msg","查询成功");
        return JSONObject.toJSONString(map);
    }
    @RequestMapping(value = "/press/getvideocomment",method = RequestMethod.GET)
    public String getvideocomment(@RequestParam("videoid")int videoid){
        return newsCommentService.findCommentByVideoId(videoid);
    }
}